import { getSblxBySbzlx } from '../../../../../mapUtils/SblxUtil';
import CONST from '../../../../../constant/Common';
import IpmsLineString from '../IpmsLineString';
import RuleResult from '../vo/RuleResult';

/**
 * 电力电缆修改
 * @implements {IpmsLineString}
 */
class PmsDldl extends IpmsLineString {
  constructor(_) {
    super(_);
    this.defaultSbzlx = 20100000;
    this.defaultDydj = 22;
    this.drawtype = 'pms_dldl';
    this.defaultName = '新增电力电缆';
    this.defaultLineColor = '#ff0000';
    this.defaultLineWidth = 3;
    this.defaultLineOpacity = 1;
  }

  /**
   * 新增电力电缆
   * @override
   * @param {Object} option 默认参数
   * @param {Array} snapList 捕捉图形
    * @param {Array} ignoreSbzlx 捕捉图形中不包含该数组中设备子类型设备
   */
  draw(option, snapList, ignoreSbzlx) {
    this.option = option;
    this.properties = Object.assign({}, this.properties, this.option);
    return super.draw(option, snapList, ignoreSbzlx);
  }

  /**
   * 编辑电力电缆
   * @override
   * @param {Object} option 默认参数
   * @param {Array} snapList 捕捉图形
   * @param {Array} ignoreSbzlx 捕捉图形中不包含该数组中设备子类型设备
   */
  edit(option, snapList, ignoreSbzlx) {
    this.option = option;
    return super.edit(option, snapList, ignoreSbzlx);
  }

  /**
   * 获取当前绘制设备信息
   * @override
   */
  async getTargetDevice() {
    let startEquip = null, endEquip = null;
    let start = this.getStartDevice();
    let end = this.getEndDevice();
    if (start) {
      if (this._isLine(start.sbzlx)) {
        // 起点设备为线设备，先创建T节点，然后将起始设备设为T节点
        let point = turf.point(this.geometry.coordinates[0]);
        let line = turf.lineString(start.geometry.coordinates, start);
        let virtualPoint = await this._getVirtualPoint(point, line, CONST.TPoint);
        startEquip = {
          oid: virtualPoint.oid,
          sbzlx: virtualPoint.sbzlx,
          versionid: virtualPoint.versionid
        };
      } else {
        // 起始设备为其他设备
        startEquip = {
          oid: start.oid,
          sbmc: start.sbmc,
          sblx: start.sblx,
          sbzlx: start.sbzlx,
          versionid: start.versionid,
          dydj: start.dydj,
        };
      }
    }
    if (end) {
      if (this._isLine(end.sbzlx)) {
        // 终点设备为线设备，先创建T节点，然后将起始设备设为T节点
        let point = turf.point(this.geometry.coordinates[this.geometry.coordinates.lenght - 1]);
        let line = turf.lineString(end.geometry.coordinates, end);
        let virtualPoint = await this._getVirtualPoint(point, line, CONST.TPoint);
        endEquip = {
          oid: virtualPoint.oid,
          sbzlx: virtualPoint.sbzlx,
          versionid: virtualPoint.versionid
        };
      } else {
        // 终点设备为其他设备
        endEquip = {
          oid: end.oid,
          sbmc: end.sbmc,
          sblx: end.sblx,
          sbzlx: end.sbzlx,
          versionid: end.versionid,
          dydj: start.dydj,
        };
      }
    } else if (!this.editFlags.status) {
      // 终点设备不存在，将终点设备设为虚拟节点
      let point = turf.point(this.geometry.coordinates[this.geometry.coordinates.length - 1]);
      endEquip = {
        sbzlx: CONST.tempPoint,
        shape: this.getGeometryWkt(point.geometry)
      };
    }
    let data = {
      startEquip: startEquip,
      endEquip: endEquip,
      schema: this.option.schema,
      ghrw: this.option.ghrw || '',
      ghfa: this.option.ghfa || '',
      ghnf: this.option.ghnf || '',
      ghxm: this.option.ghxm || '',
      fxlx: this.option.fxlx || 2,
      oid: this.option.oid || '',
      away: 0,
      sblx: getSblxBySbzlx(this.defaultSbzlx),
      sbzlx: this.defaultSbzlx,
      otherfields: {
        sbmc: this.defaultName,
        shape: this.getGeometryWkt(this.geometry),
        dxcd: turf.length(this.geometry),
        dydj: this.defaultDydj,
        ssqy: this.option.ssqy || '',
        ssdkx: this.option.ssdkx || ''
      }
    };
    if (this.editFlags.status === 'editShape') {
      data.shapeList = [{
        oid: this.option.oid,
        sbzlx: this.option.sbzlx,
        blockid: this.option.blockid,
        versionid: this.option.versionid,
        shape: data.otherfields.shape
      }];
    }
    return data;
  }

  /**
   * 新增电力电缆验证点合理性
   * @override
   * @param {Number} index 点下标
   * @param {*} snap 捕捉到的设备
   */
  async _ruleJudgeDrawPoint(index, snap) {
    let layer = snap ? snap.layer : null;
    // let layer = snap.layer;
    if (!layer) {
      // 如果是空点
      if (index === 0) {
        // 如果是第一个点，不能为空
        return new RuleResult(false, index, '起点设备为空');
      }
      // 其它点可以为空
      return new RuleResult(true, index, 'success');

    }
    // 如果不是空点
    if (this._isStation(layer.properties.sbzlx)) {
      // 先判断是否是电站，判断电站间隔是否已用完
      // let isDzjgAvailable = await this._isDzjgAvailable(layer.properties, this.defaultDydj);
      // if (isDzjgAvailable) {
      return new RuleResult(true, index, 'success');
      // } else {
      //   return new RuleResult(false, index, "电站无可用间隔，无法进行线路录入");
      // }
      // } else if (!this._isPermittedDevice(layer.properties.sbzlx)) {
      //   // 然后判断是否是柱上设备
      //   return new RuleResult(false, index, "连接设备为柱上设备");
    } else if (this._isLine(layer.properties.sbzlx)) {
      // 再判断是否是线路，线路是否有足够线段T接
      let segments = layer.properties.ysb_oid.split(';');
      if (segments.length <= 1) {
        return new RuleResult(false, index, '线路没有多余连接点进行T接');
      }
      return new RuleResult(true, index, 'success');

    }
    // 其它情况直接通过
    return new RuleResult(true, index, 'success');


  }

  /**
   * 验证编辑点的合理性
   * @override
   * @param {*} snap 捕捉到的设备
   */
  async _ruleJudgeEditPoint(snap) {
    if (!snap) {
      return new RuleResult(false, 0, '连接设备为空');
    }
    // 如果不是空点
    if (this._isStation(snap.properties.sbzlx)) {
      // 先判断是否是电站，判断电站间隔是否已用完
      // let isDzjgAvailable = await this._isDzjgAvailable(snap.properties, this.defaultDydj);
      // if (isDzjgAvailable) {
      return new RuleResult(true, 0, 'success');
      // } else {
      //   return new RuleResult(false, 0, "电站无可用间隔，无法进行线路录入");
      // }
      // } else if (!this._isPermittedDevice(layer.properties.sbzlx)) {
      //   // 然后判断是否是柱上设备
      //   return new RuleResult(false, index, "连接设备为柱上设备");
    } else if (this._isLine(snap.properties.sbzlx)) {
      // 再判断是否是线路，线路是否有足够线段T接
      let segments = snap.properties.ysb_oid.split(';');
      if (segments.length <= 1) {
        return new RuleResult(false, 0, '线路没有多余连接点进行T接');
      }
      return new RuleResult(true, 0, 'success');

    }
    // 其它情况直接通过
    return new RuleResult(true, 0, 'success');


  }

  /**
   * 验证所有点合理性
   * @override
   */
  _ruleJudgeAllPoint() {
    const EnableStartSbzlx = [10200002, 30000004, 32400000, 20400000, 30000005, 32300000, 30000099, 30000003, 30000010, 30000002, 34300000, 30000006, 30000001, 30000000];
    const EnableEndSbzlx = [10200002, 30000004, 32400000, 20400000, 30000005, 32300000, 30000099, 30000003, 30000010, 30000002, 34300000, 30000006, 30000001, 30000000];
    
    let start = this.snaps[0];
    let end = this.snaps[this.snaps.length - 1];
    if(!start) {

      return new RuleResult(false, 0, '起点必须连接站房');
    }
    if(end) {
        var startSbzlx = start.properties.sbzlx;
        // 已连接起点，判断起点设备类型是否正确
        if(EnableStartSbzlx.indexOf(startSbzlx) === -1) {
            return new RuleResult(false, 0, '起点设备类型不正确');

    //   if(!this._isStation(start.properties.sbzlx)) {
        // return new RuleResult(false, 0, '起点必须连接站房');
      }
      var endSbzlx = end.properties.sbzlx;
      // 如果连接，则需要判断终点设备类型是否正确
      if (EnableEndSbzlx.indexOf(endSbzlx) === -1) {
          return new RuleResult(false, 0, "终点设备类型不正确");
    //   if(!this._isStation(end.properties.sbzlx)) {
    //     return new RuleResult(false, 0, '终点必须连接站房');
      }
      return new RuleResult(true, 0, 'success');
    }
    if(this.option.dydj + '' !== '22') {
      // 除了10kv电缆，其他电压等级都需要连接终点设备
      return new RuleResult(false, 0, '终点必须连接站房');
    }
    return new RuleResult(true, 0, 'success');


  }

  /**
   * 是否是允许捕捉的设备
   * @param {Number} sbzlx 设备子类型
   */
  _isPermittedDevice(sbzlx) {
    // 不能捕捉柱上设备（柱上断路器、柱上负荷开关、柱上隔离开关）
    let notPermittedDeviceSbzlx = [11100000, 11200000, 11300000];
    return notPermittedDeviceSbzlx.indexOf(sbzlx) < 0;
  }
}

export default PmsDldl;
